package com.atguigu.spring.config;

import com.atguigu.spring.entity.Address;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class AddressConverter implements Converter<String, Address> {
    @Override
    public Address convert(String source) {

        // 判空保护
        if (source == null || source.trim().length() == 0) {
            throw new RuntimeException("源字符串不能为空！！！");
        }

        // 针对源字符串进行拆分
        String[] split = source.split(",");
        String province = split[0];
        String city = split[1];
        String street = split[2];

        return new Address(province, city, street);
    }
}
